import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * @date 2018-12-17
 */
import { getResponse, createPagination } from 'utils/utils';
import { queryMapIdpValue, getPublicKey as _getPublicKey } from 'cpaas-front/lib/services/api'; // 相对路径

import { subAccountQueryPage, // 查询子账户-分页
subAccountPasswordUpdate, // 修改别人的密码
// subAccountPasswordUpdateSelf, // 自己修改密码
subAccountUserRolesQuery, // 查询可分配角色
subAccountCurrentUserRoles, // 查询当前用户已分配角色
subAccountQuery, // 查询详情
subAccountCreate, // 新建
subAccountUpdate, // 更新
subAccountUserUnlock, // 解锁
subAccountUserRolesRemove, // 解锁
subAccountCurrentGroupRoles, subAccountUserGroupsQuery, addUserGroup as _addUserGroup, deleteUserGroup as _deleteUserGroup, queryDimension as _queryDimension, queryEmployee as _queryEmployee } from "../services/subAccountService";
import { getPasswordRule as _getPasswordRule } from "../services/commonService";
export default {
  namespace: 'subAccount',
  state: {
    dataSource: [],
    // 子账户数据
    pagination: false,
    // 子账户分页信息
    currentUserRoles: [],
    // 当前编辑用户 已经分配的角色
    lov: {},
    // 存在 值集 lov 相关的数据
    passwordTipMsg: {},
    publicKey: '',
    // 公钥
    dimensionList: [],
    employeeList: [],
    employeePagination: {}
  },
  effects: {
    // 初始化 值集 这类在页面生存周期不会变的变量
    init: /*#__PURE__*/_regeneratorRuntime.mark(function init(_, _ref) {
      var call, put, lovBatchRes, lovBatch, levelMap;
      return _regeneratorRuntime.wrap(function init$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              call = _ref.call, put = _ref.put;
              _context.next = 3;
              return call(queryMapIdpValue, {
                level: 'CIAM.RESOURCE_LEVEL',
                // 层级
                authorityType: 'CIAM.AUTHORITY_TYPE_CODE',
                // 授权方式
                idd: 'CPFM.IDD',
                // 国际化手机号前缀
                gender: 'CPFM.GENDER',
                // 性别
                userType: 'CIAM.USER_TYPE'
              });

            case 3:
              lovBatchRes = _context.sent;
              lovBatch = getResponse(lovBatchRes);

              if (!lovBatch) {
                _context.next = 10;
                break;
              }

              levelMap = {};
              lovBatch.level.forEach(function (level) {
                levelMap[level.value] = level;
              });
              _context.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  lov: {
                    level: lovBatch.level,
                    levelMap: levelMap,
                    authorityType: lovBatch.authorityType,
                    idd: lovBatch.idd,
                    gender: lovBatch.gender,
                    userType: lovBatch.userType
                  }
                }
              });

            case 10:
            case "end":
              return _context.stop();
          }
        }
      }, init);
    }),
    getPublicKey: /*#__PURE__*/_regeneratorRuntime.mark(function getPublicKey(_, _ref2) {
      var call, put, res;
      return _regeneratorRuntime.wrap(function getPublicKey$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              call = _ref2.call, put = _ref2.put;
              _context2.next = 3;
              return call(_getPublicKey);

            case 3:
              res = _context2.sent;

              if (!res) {
                _context2.next = 7;
                break;
              }

              _context2.next = 7;
              return put({
                type: 'updateState',
                payload: {
                  publicKey: res.publicKey
                }
              });

            case 7:
              return _context2.abrupt("return", res);

            case 8:
            case "end":
              return _context2.stop();
          }
        }
      }, getPublicKey);
    }),
    // 获取密码校验规则
    getPasswordRule: /*#__PURE__*/_regeneratorRuntime.mark(function getPasswordRule(_ref3, _ref4) {
      var payload, call, put, res;
      return _regeneratorRuntime.wrap(function getPasswordRule$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call, put = _ref4.put;
              _context3.t0 = getResponse;
              _context3.next = 5;
              return call(_getPasswordRule, payload);

            case 5:
              _context3.t1 = _context3.sent;
              res = (0, _context3.t0)(_context3.t1);

              if (!res) {
                _context3.next = 10;
                break;
              }

              _context3.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  passwordTipMsg: res
                }
              });

            case 10:
              return _context3.abrupt("return", res);

            case 11:
            case "end":
              return _context3.stop();
          }
        }
      }, getPasswordRule);
    }),
    fetchSubAccountList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchSubAccountList(_ref5, _ref6) {
      var payload, call, put, res, subAccount;
      return _regeneratorRuntime.wrap(function fetchSubAccountList$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call, put = _ref6.put;
              _context4.next = 4;
              return call(subAccountQueryPage, payload);

            case 4:
              res = _context4.sent;
              subAccount = getResponse(res);

              if (!subAccount) {
                _context4.next = 9;
                break;
              }

              _context4.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  dataSource: subAccount.content,
                  pagination: createPagination(subAccount)
                }
              });

            case 9:
            case "end":
              return _context4.stop();
          }
        }
      }, fetchSubAccountList);
    }),
    updatePassword: /*#__PURE__*/_regeneratorRuntime.mark(function updatePassword(_ref7, _ref8) {
      var payload, call, res, pRes;
      return _regeneratorRuntime.wrap(function updatePassword$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call;
              _context5.next = 4;
              return call(subAccountPasswordUpdate, payload);

            case 4:
              res = _context5.sent;
              pRes = getResponse(res);
              return _context5.abrupt("return", pRes);

            case 7:
            case "end":
              return _context5.stop();
          }
        }
      }, updatePassword);
    }),
    // 查询 当前用户 拥有的角色
    getCurrentUserRoles: /*#__PURE__*/_regeneratorRuntime.mark(function getCurrentUserRoles(_ref9, _ref10) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function getCurrentUserRoles$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref9.payload;
              call = _ref10.call;
              _context6.next = 4;
              return call(subAccountCurrentUserRoles, payload);

            case 4:
              res = _context6.sent;
              return _context6.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context6.stop();
          }
        }
      }, getCurrentUserRoles);
    }),
    // 查询当前租户可分配角色
    fetchRoles: /*#__PURE__*/_regeneratorRuntime.mark(function fetchRoles(_ref11, _ref12) {
      var payload, call, rolesRes;
      return _regeneratorRuntime.wrap(function fetchRoles$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              payload = _ref11.payload;
              call = _ref12.call;
              _context7.next = 4;
              return call(subAccountUserRolesQuery, payload);

            case 4:
              rolesRes = _context7.sent;
              return _context7.abrupt("return", getResponse(rolesRes));

            case 6:
            case "end":
              return _context7.stop();
          }
        }
      }, fetchRoles);
    }),
    querySubAccount: /*#__PURE__*/_regeneratorRuntime.mark(function querySubAccount(_ref13, _ref14) {
      var payload, call, userId, subAccountDetail;
      return _regeneratorRuntime.wrap(function querySubAccount$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              payload = _ref13.payload;
              call = _ref14.call;
              userId = payload.userId;
              _context8.next = 5;
              return call(subAccountQuery, userId);

            case 5:
              subAccountDetail = _context8.sent;
              return _context8.abrupt("return", getResponse(subAccountDetail));

            case 7:
            case "end":
              return _context8.stop();
          }
        }
      }, querySubAccount);
    }),
    createSubAccount: /*#__PURE__*/_regeneratorRuntime.mark(function createSubAccount(_ref15, _ref16) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function createSubAccount$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              payload = _ref15.payload;
              call = _ref16.call;
              _context9.next = 4;
              return call(subAccountCreate, payload);

            case 4:
              res = _context9.sent;
              return _context9.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context9.stop();
          }
        }
      }, createSubAccount);
    }),
    updateSubAccount: /*#__PURE__*/_regeneratorRuntime.mark(function updateSubAccount(_ref17, _ref18) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function updateSubAccount$(_context10) {
        while (1) {
          switch (_context10.prev = _context10.next) {
            case 0:
              payload = _ref17.payload;
              call = _ref18.call;
              _context10.next = 4;
              return call(subAccountUpdate, payload);

            case 4:
              res = _context10.sent;
              return _context10.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context10.stop();
          }
        }
      }, updateSubAccount);
    }),
    // 解锁用户
    unlockSubAccount: /*#__PURE__*/_regeneratorRuntime.mark(function unlockSubAccount(_ref19, _ref20) {
      var payload, call, userId, res;
      return _regeneratorRuntime.wrap(function unlockSubAccount$(_context11) {
        while (1) {
          switch (_context11.prev = _context11.next) {
            case 0:
              payload = _ref19.payload;
              call = _ref20.call;
              userId = payload.userId;
              _context11.next = 5;
              return call(subAccountUserUnlock, userId);

            case 5:
              res = _context11.sent;
              return _context11.abrupt("return", getResponse(res));

            case 7:
            case "end":
              return _context11.stop();
          }
        }
      }, unlockSubAccount);
    }),
    // 删除角色
    removeRoles: /*#__PURE__*/_regeneratorRuntime.mark(function removeRoles(_ref21, _ref22) {
      var payload, call, memberRoleList, res;
      return _regeneratorRuntime.wrap(function removeRoles$(_context12) {
        while (1) {
          switch (_context12.prev = _context12.next) {
            case 0:
              payload = _ref21.payload;
              call = _ref22.call;
              memberRoleList = payload.memberRoleList;
              _context12.next = 5;
              return call(subAccountUserRolesRemove, memberRoleList);

            case 5:
              res = _context12.sent;
              return _context12.abrupt("return", getResponse(res));

            case 7:
            case "end":
              return _context12.stop();
          }
        }
      }, removeRoles);
    }),
    // 查询 当前用户 已分配的用户组
    getCurrentUserGroups: /*#__PURE__*/_regeneratorRuntime.mark(function getCurrentUserGroups(_ref23, _ref24) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function getCurrentUserGroups$(_context13) {
        while (1) {
          switch (_context13.prev = _context13.next) {
            case 0:
              payload = _ref23.payload;
              call = _ref24.call;
              _context13.next = 4;
              return call(subAccountCurrentGroupRoles, payload);

            case 4:
              res = _context13.sent;
              return _context13.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context13.stop();
          }
        }
      }, getCurrentUserGroups);
    }),
    // 查询当前租户可分配的用户组
    fetchGroups: /*#__PURE__*/_regeneratorRuntime.mark(function fetchGroups(_ref25, _ref26) {
      var payload, call, rolesRes;
      return _regeneratorRuntime.wrap(function fetchGroups$(_context14) {
        while (1) {
          switch (_context14.prev = _context14.next) {
            case 0:
              payload = _ref25.payload;
              call = _ref26.call;
              _context14.next = 4;
              return call(subAccountUserGroupsQuery, payload);

            case 4:
              rolesRes = _context14.sent;
              return _context14.abrupt("return", getResponse(rolesRes));

            case 6:
            case "end":
              return _context14.stop();
          }
        }
      }, fetchGroups);
    }),
    // 添加用户组
    addUserGroup: /*#__PURE__*/_regeneratorRuntime.mark(function addUserGroup(_ref27, _ref28) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function addUserGroup$(_context15) {
        while (1) {
          switch (_context15.prev = _context15.next) {
            case 0:
              payload = _ref27.payload;
              call = _ref28.call;
              _context15.next = 4;
              return call(_addUserGroup, payload);

            case 4:
              res = _context15.sent;
              return _context15.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context15.stop();
          }
        }
      }, addUserGroup);
    }),
    // 删除用户组
    deleteUserGroup: /*#__PURE__*/_regeneratorRuntime.mark(function deleteUserGroup(_ref29, _ref30) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function deleteUserGroup$(_context16) {
        while (1) {
          switch (_context16.prev = _context16.next) {
            case 0:
              payload = _ref29.payload;
              call = _ref30.call;
              _context16.next = 4;
              return call(_deleteUserGroup, payload);

            case 4:
              res = _context16.sent;
              return _context16.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context16.stop();
          }
        }
      }, deleteUserGroup);
    }),
    // 获取权限维度
    queryDimension: /*#__PURE__*/_regeneratorRuntime.mark(function queryDimension(_ref31, _ref32) {
      var payload, call, put, res;
      return _regeneratorRuntime.wrap(function queryDimension$(_context17) {
        while (1) {
          switch (_context17.prev = _context17.next) {
            case 0:
              payload = _ref31.payload;
              call = _ref32.call, put = _ref32.put;
              _context17.t0 = getResponse;
              _context17.next = 5;
              return call(_queryDimension, payload);

            case 5:
              _context17.t1 = _context17.sent;
              res = (0, _context17.t0)(_context17.t1);

              if (!res) {
                _context17.next = 10;
                break;
              }

              _context17.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  dimensionList: res
                }
              });

            case 10:
              return _context17.abrupt("return", res);

            case 11:
            case "end":
              return _context17.stop();
          }
        }
      }, queryDimension);
    }),
    queryEmployee: /*#__PURE__*/_regeneratorRuntime.mark(function queryEmployee(_ref33, _ref34) {
      var payload, call, put, res, employeeList;
      return _regeneratorRuntime.wrap(function queryEmployee$(_context18) {
        while (1) {
          switch (_context18.prev = _context18.next) {
            case 0:
              payload = _ref33.payload;
              call = _ref34.call, put = _ref34.put;
              _context18.next = 4;
              return call(_queryEmployee, payload);

            case 4:
              res = _context18.sent;
              employeeList = getResponse(res);

              if (!employeeList) {
                _context18.next = 9;
                break;
              }

              _context18.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  employeeList: employeeList.content,
                  employeePagination: createPagination(employeeList)
                }
              });

            case 9:
            case "end":
              return _context18.stop();
          }
        }
      }, queryEmployee);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref35) {
      var payload = _ref35.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};